package ModelImplementation

import (
	"SPC/Model"
	"time"
)

type HourlyClassification struct {
	itsRate      float64
	itsTimeCards map[time.Time]TimeCard
}

//用一个全局变量来模拟静态变量，所有card操作都是在操作它
var card *HourlyClassification = &HourlyClassification{
	itsRate: 0,
	itsTimeCards: make(map[time.Time]TimeCard),
}

//获取该全局对象
func GetHourlyClassification() *HourlyClassification {
	return card
}

func NewHourlyClassification(hourlyRate float64) *HourlyClassification {
	return &HourlyClassification{
		hourlyRate,
		make(map[time.Time]TimeCard),
	}
}

func (h *HourlyClassification) GetRate() float64 {
	return h.itsRate
}

func (h *HourlyClassification) GetTimeCard(date time.Time) TimeCard {
	return h.itsTimeCards[date]
}

func (h *HourlyClassification) AddTimeCard(tc TimeCard) {
	h.itsTimeCards[tc.GetDate()] = tc
}

func (h *HourlyClassification) CalculatePayForTimeCard(tc TimeCard) float64 {
	hours := tc.GetHours()
	overtime := Max(0.0, hours-8.0)
	straightTime := hours - overtime
	return straightTime*h.itsRate+overtime*h.itsRate*1.5
}

func (h *HourlyClassification)CalculatePay(pc Model.Paycheck)float64{
	totalPay := 0.0
	// payPeriodEndDate := pc.GetPayPeriodEndDate()
	for k, _ := range h.itsTimeCards{
		tc := h.itsTimeCards[k]

		dt1 := pc.GetPayPeriodStartDate()
		dt2 := pc.GetPayPeriodEndDate()
		if (k.After(dt1) && k.Before(dt2)) {
			totalPay += h.CalculatePayForTimeCard(tc)
		}
	}
	return totalPay
}

func Max(nums ...float64) float64 {
	var maxNum float64 = 0
	for _, num := range nums {
		if num > maxNum {
			maxNum = num
		}
	}
	return maxNum
}
